import {App, Builder, Configs, Database, Dev, Directorys, E3Object, Env, UserData} from './types'

/** 定义总配置 */
const defineE3Config = (configs: Configs): Configs => configs

/** 定义环境名称配置 */
const defineE3ConfigEnv = (configs: Env): Env => configs

/** 定义目录配置 */
const defineE3ConfigDirectorys = (configs: Directorys): Directorys => configs

/** 定义开发配置 */
const defineE3ConfigDev = (configs: Dev): Dev => configs

/** 定义APP配置 */
const defineE3ConfigApp = (configs: App): App => configs

/** 定义打包配置 */
const defineE3ConfigBuilder = (configs: Builder): Builder => configs

/** 定义用户数据配置 */
const defineE3ConfigUserData = (configs: UserData): UserData => configs

/** 定义数据库配置 */
const defineE3ConfigDatabase = (configs: Database): Database => configs

/** 合并配置 */
const mergeE3Config = <T extends E3Object, U extends E3Object>(target: T, source: U): T & U => {
    if (typeof target !== 'object' || target === null) {
        return source as T & U
    }
    
    if (typeof source !== 'object' || source === null) {
        return source as T & U
    }
    
    // 特殊处理：如果 source 是数组，直接覆盖 target
    if (Array.isArray(source)) {
        return source as unknown as T & U
    }
    
    const output = {...target} as T & U
    
    for (const key in source) {
        if (source.hasOwnProperty(key)) {
            if (key in target) {
                output[key] = mergeE3Config(target[key] as any, source[key] as any)
            } else {
                output[key] = source[key] as any
            }
        }
    }
    return output
}

export {
    defineE3Config,
    defineE3ConfigEnv,
    defineE3ConfigDirectorys,
    defineE3ConfigDev,
    defineE3ConfigApp,
    defineE3ConfigBuilder,
    defineE3ConfigUserData,
    defineE3ConfigDatabase,
    mergeE3Config
}
